package com.zw.blog.web;

import com.zw.blog.po.Tag;
import com.zw.blog.service.BlogService;
import com.zw.blog.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class TagShowController { // Spring下不能有重名的类

    @Autowired
    private TagService tagService;

    @Autowired
    private BlogService blogService;

    @GetMapping("/tags/{id}")
    public String tag(@PageableDefault(size = 5, sort = {"updateTime"}, direction = Sort.Direction.DESC) Pageable pageable,
                      @PathVariable Long id, Model model, HttpSession session) { //传递当前活跃的类型id
        List<Tag> tags = tagService.listTagTop(1000);

        for (Tag tag : tags) {
            tag.initPublishedBlogs(session);
        }
        if (id == -1) { //说明第一次进页面，没有选中类型
            id = tags.get(0).getId();
        }
        model.addAttribute("tags", tags);
        model.addAttribute("page", blogService.listBlogPublished(id, session, pageable));
        model.addAttribute("activeTagId", id);
        return "tags";
    }
}
